
""" The Galaxy class is the interface to almost all the game data (or it is until 
I figure out its bad or a better way to do it). IT takes args for the width and height
of the map grid, and how dense the starfield will be. The class (upon init) generates 
the map and uses the generated map to populate a list with Star objects. Each Star 
has its own internals of planet generation etc.
"""

from random import *
from Star import *
from Planet import *
from Ksink import *

class Galaxy:
        def __init__(self, w_, h_, dense_):
                # calles the loadNames() function in Ksink.
                self.starNames = loadNames()
                
                self.sizeW = w_
                self.sizeH = h_
                
                # generate the galaxy map and store it in a temp var
                map = generateMap(w_, h_, dense_)
                
                # init a container for the stars then call populateMap to fill it.
                self.stars = []
                self.stars = populateMap(w_, h_, map, self.starNames)
        
        # Method to return a star from a map location
        # TODO: finish the meat of this code
        def     getStar(self, x_, y_):
                tempPos = (x_,y_)
                for star in stars:
                        if star.pos == temp.pos:
                                return star
                        else:
                                return None
                
        # Used to simulate a multi-dimensional array. can get the value of a flat list as if it had x/y cords   
        def getMapLoc(self, x_, y_):
                return (x_+(y_*self.getHeight()))+1
        
        # returns the width of the map.
        def getWidth(self):
                return self.sizeW
        
        # returns the height of the map
        def getHeight(self):
                return self.sizeH               
        
        # Dumps the map to the screen along with data on each star and its planets.
        def dump(self):
                print "map info: size: [", self.getWidth(), ",", self.getHeight(), "] length: ", len(self.map)
                x = 0
                y = 0
                while y < self.sizeH:
                        while x < self.sizeW:
                                print self.map[self.getMapLoc(x,y)],
                                x += 1
                        print ""
                        x = 0
                        y += 1
                printTotals(self)

        def generateMapNew(self, w_, h_, totalStars_):
            for iteration in range(1000):
                map = starMap(w_, h_, totalStars_)
                

                
class starPoints:
        def __init__(self, w_, h_, totalStars_):
                self.starPointList = []

                for i in range(totalStars_):
                        self.starPointList.append((randint(1,w_),randint(1,h_)))
        def getDistance(self, pointAlpha, pointOmega):
                return abs(pointAlpha[0] - pointOmega[0]) + abs(pointAlpha[1] - pointOmega[1]) / 2

        def isSane(self):
                return True

# generate map returns an array of len(w*h) of mostly 0s unless the random number generated is between 1 and dense_
def generateMap(w_, h_, dense_):
        
        thresh = 0
        map = []
        while thresh <= dense_*5:
                thresh = 0
                size = w_*h_
                x = 0 
                while x <= size:
                        randomNumber = randint(1, 100)
                        if randomNumber <= dense_: 
                                map.append(randomNumber)
                                thresh +=1
                        else: map.append(0)
                        x += 1  
        print "thresh", thresh
        return map                        
        

# Takes the list of map cords and populates the list of stars with real Star objects.
def populateMap(w_, h_, map_, names_):
        starsList = []
        stars = []
        x = 0
        y = 0
        while y < h_:
                while x < w_:
                        t = x+(y*h_)+1
                        if map_[t]:
                                #print "Star found @:", x, y
                                starsList.append((x, y))
                        x += 1
                x = 0
                y += 1
        for item in starsList:
                stars.append(Star(item[0], item[1], starsList, getName(names_)))
        return stars

# Prints the totals of the planets and stars, 
def printTotals(galaxy_):
        totalPlanets = 0
        totalStars = 0
        for s in galaxy_.stars:
                totalStars += 1
                print s.name, "@", s.x, s.y, "Closest:",
                for c in s.closest:
                        print c,
                print ""
                totalPlanets += len(s.planets)
                print "Planets:", len(s.planets)
        print "Total Planets:", totalPlanets
        print "Total Stars", totalStars
        
